package com.homedroid.ui;

import java.io.IOException;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import com.homedroid.dal.R;
import com.homedroid.dal.User;
import com.homedroid.tasks.GetMyListTask;

public class Home extends Activity {
	
	private Button backFromShopping, outOfSomething, myProducts;
	private User user;
	private SharedPreferences preferences;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		setContentView(R.layout.home);
		super.onCreate(savedInstanceState);
		
		this.backFromShopping = (Button) findViewById(R.id.btn_backFromShopping);
		this.backFromShopping.setOnClickListener(listener);
		
		this.outOfSomething = (Button) findViewById(R.id.btn_outOfSomething);
		this.outOfSomething.setOnClickListener(listener);
		
		this.myProducts = (Button) findViewById(R.id.btn_myProducts);
		this.myProducts.setOnClickListener(listener);
		
		preferences = PreferenceManager.getDefaultSharedPreferences(this);
		
		user = new User(preferences.getString("username", "username"),
				preferences.getString("password", "password"));
		
	}

	public Button.OnClickListener listener = new Button.OnClickListener() {

		@Override
		public void onClick(View arg0) {
			switch (arg0.getId()) {
			case R.id.btn_backFromShopping:
				startActivity(new Intent(Home.this, List1.class));
				break;
			case R.id.btn_outOfSomething:
				startActivity(new Intent(Home.this, List2.class));
				break;
			case R.id.btn_myProducts:
				try {
					GetMyListTask getmylisttask = new GetMyListTask(Home.this, user);
					getmylisttask.execute();
				} catch (IOException e) {
					Toast.makeText(Home.this, R.string.txt_noconnection, Toast.LENGTH_SHORT).show();
//					Log.w("ScanedTask", R.string.txt_noconnection);
				}
				break;

			default:
				break;
			}
			
		}
		
	};
}
